<?php
namespace Admin\Controller;
use Think\Controller;
class OrderController extends Controller {

	public function _initialize()
    {
        //未登录、登录超时、长时间未动
    	if (empty($_SESSION['adminid']) || time() > $_SESSION['adminovertime'] || empty($_SESSION['adminovertime']) ) {

    		$this->redirect('/admin.php/Common/login');
    	}

        session('adminovertime',(time()+60*60*C('adminovertime')));

    }


    /**
     * [buycenter 购置中心积分]
     * @return [type] [description]
     */
    public function buycenter(){

    	$m_sellcenter = M("Sell_center");
        $m_ordercenter = M('Order_center');
        $m_user = M('User');

        $count = $m_ordercenter->count();
        $Page  = new \Think\Page($count,30);
        $show  = $Page->adminshow();

        
        $ordercenterList = $m_ordercenter->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        foreach ($ordercenterList as $key => $value) {
        	$where_user['id'] = $value['user_id'];
        	$userInfo = $m_user->where($where_user)->field('account,phone')->find();
        	$ordercenterList[$key]['account'] = $userInfo['account'];
            $ordercenterList[$key]['phone'] = $userInfo['phone'];
        }


        $sellcenterList = $m_sellcenter->order('id desc')->select();

        $this->assign('page',$show);
        $this->assign('ordercenterList',$ordercenterList);
        $this->assign('sellcenterList',$sellcenterList);
    	$this->display();
    }


    /**
     * [show 查看凭证]
     * @return [type] [description]
     */
    public function show(){
    	$id = $_REQUEST['id'];

    	$m_ordercenter = M('Order_center');

    	$where_order['id'] = $id;
    	$pic_url = $m_ordercenter->where($where_order)->getfield('pic_url');

    	
    	$this->assign('pic_url',$pic_url);
    	$this->display();
    }


    /**
     * [ctrl_buyCenterFinish 完成中心积分]
     * @return [type] [description]
     */
    public function ctrl_buyCenterFinish(){
    	if(IS_AJAX){
    		$id = $_REQUEST['id'];

    		$m_ordercenter = M('Order_center');
    		$m_user = M('User');

    		$where_order['id'] = $id;
    		$ordercenterInfo = $m_ordercenter->where($where_order)->find();

    		if ($ordercenterInfo['status'] == 2) {
    			$this->ajaxReturn(array(
                    'code'=>1,
                    'msg' => '已完成，请勿重复操作'
                ));
    		}else{
    			$save['status'] = 2;
    			$res = $m_ordercenter->where($where_order)->save($save);
    			if ($res) {
    				// 修改用户交易积分数据、添加交易积分记录
    				$where_user['id'] = $ordercenterInfo['user_id'];
    				$userInfo = $m_user->where($where_user)->find();

    				$old_center = $userInfo['score_trading'];
    				$change_center = $ordercenterInfo['num'];
    				$new_center = $old_center + $change_center;

    				$save_user['score_trading'] = $new_center;
    				$result = $m_user->where($where_user)->save($save_user);
    				if ($result) {
    					addTradingTable($userInfo['id'],$old_center,$change_center,$new_center,'购买平台交易积分，新增'.$change_center);
    				}

    				$this->ajaxReturn(array(
	                    'code'=>1,
	                    'msg' => '确认成功'
	                ));
    			}else{
    				$this->ajaxReturn(array(
	                    'code'=>0,
	                    'msg' => '操作失败'
	                ));
    			}
    		}

    	}else{
    		$this->redirect('/admin.php/Common/BadRequest');
    	}
    }


    /**
     * [ctrl_addSellcenter 发布平台中心积分]
     * @return [type] [description]
     */
    public function ctrl_addSellcenter(){
    	if (IS_AJAX) {
    		// print_r($_REQUEST);die();
    		$num = trim($_REQUEST['num']);
    		$discount = trim($_REQUEST['discount']);
    		$num_usdt = trim($_REQUEST['num_usdt']);
    		$wallet_address = trim($_REQUEST['wallet_address']);
            $phone = trim($_REQUEST['phone']);

    		$m_sellcenter = M('Sell_center');

    		if (empty($num) || empty($discount) || empty($num_usdt) || empty($wallet_address) || empty($phone)) {
    			$this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '信息不完整，请完善'
                ));
    		}else{
    			$where['num'] = $num;
	    		$where['discount'] = $discount;
	    		$where['num_usdt'] = $num_usdt;
	    		

	    		$sellcenterInfo = $m_sellcenter->where($where)->find();
	    		if ($sellcenterInfo['id']) {
	    			$this->ajaxReturn(array(
	                    'code'=>0,
	                    'msg' => '已添加，请勿重复操作'
	                ));
	    		}else{
	    			$where['wallet_address'] = $wallet_address;
                    $where['phone'] = $phone;
	    			$res = $m_sellcenter->add($where);
	    			if ($res) {
	    				$this->ajaxReturn(array(
		                    'code'=>1,
		                    'msg' => '操作成功'
		                ));
	    			}else{
	    				$this->ajaxReturn(array(
	                    	'code'=>0,
		                    'msg' => '操作失败'
		                ));
	    			}
	    		}
    		}
    	}else{
    		$this->redirect('/admin.php/Common/BadRequest');
    	}
    }


    /**
     * [ctrl_deleteCenter 删除中心积分]
     * @return [type] [description]
     */
    public function ctrl_deleteCenter(){
    	if(IS_AJAX){
    		$id = $_REQUEST['id'];

    		$m_sellcenter = M('Sell_center');

    		$where['id'] = $id;
    		$sellcenterInfo = $m_sellcenter->where($where)->find();
    		if ($sellcenterInfo['id']) {
    			$res = $m_sellcenter->where($where)->delete();
    			if ($res) {
    				$this->ajaxReturn(array(
	                	'code'=>1,
	                    'msg' => '删除成功'
	                ));
    			}else{
    				$this->ajaxReturn(array(
	                	'code'=>0,
	                    'msg' => '删除失败'
	                ));
    			}
    			
    		}else{
    			$this->ajaxReturn(array(
                	'code'=>1,
                    'msg' => '已删除，请勿重复操作'
                ));
    		}

    	}else{
    		$this->redirect('/admin.php/Common/BadRequest');
    	}
    }


    // 留言页面
    public function back(){

        $this->display();
    }


    // 退回交易积分操作
    public function ctrl_backcenter(){
        if(IS_AJAX){
            $id = $_REQUEST['id'];
            $content = trim($_REQUEST['content']);

            $m_ordercenter = M("Order_center");

            $where['id'] = $id;
            $ordercenterInfo = $m_ordercenter->where($where)->find();

            if ($ordercenterInfo['status'] == 2) {
                $this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '订单已完成，无法退回'
                ));
            }else{
                if ($ordercenterInfo['status'] == 0) {
                    $this->ajaxReturn(array(
                        'code'=>1,
                        'msg' => '订单已退回'
                    ));
                }else{
                    $save['status'] = 0;
                    $save['remark'] = $content;
                    $res = $m_ordercenter->where($where)->save($save);
                    if ($res) {
                        $this->ajaxReturn(array(
                            'code'=>1,
                            'msg' => '退回成功'
                        ));
                    }else{
                        $this->ajaxReturn(array(
                            'code'=>0,
                            'msg' => '退回失败'
                        ));
                    }
                }
            }

        }else{
            $this->redirect('/admin.php/Common/BadRequest');
        }
    }




    /**
     * [buykey 购置秘钥]
     * @return [type] [description]
     */
    public function buykey(){

    	$m_sellkey = M("Sell_key");
        $m_orderkey = M('Order_key');
        $m_user = M('User');

        $count = $m_orderkey->count();
        $Page  = new \Think\Page($count,30);
        $show  = $Page->adminshow();

        
        $orderkeyList = $m_orderkey->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        foreach ($orderkeyList as $key => $value) {
        	$where_user['id'] = $value['user_id'];
        	$userInfo = $m_user->where($where_user)->field('account,phone')->find();
        	$orderkeyList[$key]['account'] = $userInfo['account'];
            $orderkeyList[$key]['phone'] = $userInfo['phone'];
        }


        $sellkeyList = $m_sellkey->order('id desc')->select();

        $this->assign('page',$show);
        $this->assign('orderkeyList',$orderkeyList);
        $this->assign('sellkeyList',$sellkeyList);
    	$this->display();
    }



    /**
     * [ctrl_addSellkey 发布平台秘钥]
     * @return [type] [description]
     */
    public function ctrl_addSellkey(){
    	if (IS_AJAX) {
    		// print_r($_REQUEST);die();
    		$num = trim($_REQUEST['num']);
    		$discount = trim($_REQUEST['discount']);
    		$num_usdt = trim($_REQUEST['num_usdt']);
    		$wallet_address = trim($_REQUEST['wallet_address']);
            $phone = trim($_REQUEST['phone']);

    		$m_sellkey = M('Sell_key');

    		if (empty($num) || empty($discount) || empty($num_usdt) || empty($wallet_address) || empty($phone)) {
    			$this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '信息不完整，请完善'
                ));
    		}else{
    			$where['num'] = $num;
	    		$where['discount'] = $discount;
	    		$where['num_usdt'] = $num_usdt;
	    		

	    		$sellkeyInfo = $m_sellkey->where($where)->find();
	    		if ($sellkeyInfo['id']) {
	    			$this->ajaxReturn(array(
	                    'code'=>0,
	                    'msg' => '已添加，请勿重复操作'
	                ));
	    		}else{
	    			$where['wallet_address'] = $wallet_address;
                    $where['phone'] = $phone;
	    			$res = $m_sellkey->add($where);
	    			if ($res) {
	    				$this->ajaxReturn(array(
		                    'code'=>1,
		                    'msg' => '操作成功'
		                ));
	    			}else{
	    				$this->ajaxReturn(array(
	                    	'code'=>0,
		                    'msg' => '操作失败'
		                ));
	    			}
	    		}
    		}
    	}else{
    		$this->redirect('/admin.php/Common/BadRequest');
    	}
    }



    /**
     * [ctrl_deleteKey 删除秘钥]
     * @return [type] [description]
     */
    public function ctrl_deleteKey(){
    	if(IS_AJAX){
    		$id = $_REQUEST['id'];

    		$m_sellkey = M('Sell_key');

    		$where['id'] = $id;
    		$sellkeyInfo = $m_sellkey->where($where)->find();
    		if ($sellkeyInfo['id']) {
    			$res = $m_sellkey->where($where)->delete();
    			if ($res) {
    				$this->ajaxReturn(array(
	                	'code'=>1,
	                    'msg' => '删除成功'
	                ));
    			}else{
    				$this->ajaxReturn(array(
	                	'code'=>0,
	                    'msg' => '删除失败'
	                ));
    			}
    			
    		}else{
    			$this->ajaxReturn(array(
                	'code'=>1,
                    'msg' => '已删除，请勿重复操作'
                ));
    		}

    	}else{
    		$this->redirect('/admin.php/Common/BadRequest');
    	}
    }



    /**
     * [ctrl_buyKeyFinish 完成秘钥]
     * @return [type] [description]
     */
    public function ctrl_buyKeyFinish(){
    	if(IS_AJAX){
    		$id = $_REQUEST['id'];

    		$m_orderkey = M('Order_key');
    		$m_user = M('User');

    		$where_order['id'] = $id;
    		$orderkeyInfo = $m_orderkey->where($where_order)->find();

    		if ($orderkeyInfo['status'] == 2) {
    			$this->ajaxReturn(array(
                    'code'=>1,
                    'msg' => '已完成，请勿重复操作'
                ));
    		}else{
    			$save['status'] = 2;
    			$res = $m_orderkey->where($where_order)->save($save);
    			if ($res) {
    				// 修改用户动态秘钥数据、添加动态秘钥记录
    				$where_user['id'] = $orderkeyInfo['user_id'];
    				$userInfo = $m_user->where($where_user)->find();

    				$old_dynamic = $userInfo['key_dynamic'];
    				$change_dynamic = $orderkeyInfo['num'];
    				$new_dynamic = $old_dynamic + $change_dynamic;

    				$save_user['key_dynamic'] = $new_dynamic;
    				$result = $m_user->where($where_user)->save($save_user);
    				if ($result) {
    					addKeyDynamicTable($userInfo['id'],$old_dynamic,$change_dynamic,$new_dynamic,'购买动态秘钥，新增'.$change_dynamic);
    				}

    				$this->ajaxReturn(array(
	                    'code'=>1,
	                    'msg' => '确认成功'
	                ));
    			}else{
    				$this->ajaxReturn(array(
	                    'code'=>0,
	                    'msg' => '操作失败'
	                ));
    			}
    		}

    	}else{
    		$this->redirect('/admin.php/Common/BadRequest');
    	}
    }



    /**
     * [show 查看凭证]
     * @return [type] [description]
     */
    public function showkey(){
    	$id = $_REQUEST['id'];

    	$m_orderkey = M('Order_key');

    	$where_order['id'] = $id;
    	$pic_url = $m_orderkey->where($where_order)->getfield('pic_url');

    	
    	$this->assign('pic_url',$pic_url);
    	$this->display();
    }



    // 退回交易积分操作
    public function ctrl_backkey(){
        if(IS_AJAX){
            $id = $_REQUEST['id'];
            $content = trim($_REQUEST['content']);

            $m_orderkey = M("Order_key");

            $where['id'] = $id;
            $orderkeyInfo = $m_orderkey->where($where)->find();

            if ($orderkeyInfo['status'] == 2) {
                $this->ajaxReturn(array(
                    'code'=>0,
                    'msg' => '订单已完成，无法退回'
                ));
            }else{
                if ($orderkeyInfo['status'] == 0) {
                    $this->ajaxReturn(array(
                        'code'=>1,
                        'msg' => '订单已退回'
                    ));
                }else{
                    $save['status'] = 0;
                    $save['remark'] = $content;
                    $res = $m_orderkey->where($where)->save($save);
                    if ($res) {
                        $this->ajaxReturn(array(
                            'code'=>1,
                            'msg' => '退回成功'
                        ));
                    }else{
                        $this->ajaxReturn(array(
                            'code'=>0,
                            'msg' => '退回失败'
                        ));
                    }
                }
            }

        }else{
            $this->redirect('/admin.php/Common/BadRequest');
        }
    }


    /**
     * [order 申请任务]
     * @return [type] [description]
     */
    public function order(){
        $m_user = M("User");
        $m_applytask = M('Applytask');

        if (IS_POST) {

            $searchtime = trim($_REQUEST['searchtime']);
            if (!empty($searchtime)) {
                $starttime = strtotime($searchtime.' 00:00:00');
                $endtime = strtotime($searchtime.'23:59:59');
                $where_user['createtime'] = array('between',array($starttime,$endtime));
                $parameter = array('starttime'=>$starttime,'endtime'=>$endtime);
            }
            
            $count = $m_applytask->where($where_user)->count();
            $Page  = new \Think\Page($count,20,$parameter);
            $show  = $Page->adminshow();
        }elseif (IS_GET) {
            if (!empty($_REQUEST['starttime'])) {
                $where_user['createtime'] = array('between',array($_REQUEST['starttime'],$_REQUEST['endtime']));
                $parameter = array('starttime'=>$_REQUEST['starttime'],'endtime'=>$_REQUEST['endtime']);
            }
            $count = $m_applytask->where($where_user)->count();
            $Page  = new \Think\Page($count,20,$parameter);
            $show  = $Page->adminshow();
        
        }

        $applytaskList = $m_applytask->where($where_user)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($applytaskList as $key => $value) {
            $where_apply['id'] = $value['user_id'];
            $userInfo = array();
            $userInfo = $m_user->where($where_apply)->field('account,truename,phone')->find();
            $applytaskList[$key]['userInfo'] = $userInfo;
            $guessArr = array();
            $guessArr = explode('-', $value['user_guess']);
            $applytaskList[$key]['guessInfo'] = $guessArr;
        }

        $this->assign('page',$show);
        $this->assign('applytaskList',$applytaskList);
        $this->display();
    }

}